package com.freewater.cargotracker.infrastructure.persistence.mybatis.converter;

import com.freewater.cargotracker.domain.model.location.Location;
import com.freewater.cargotracker.domain.model.voyage.CarrierMovement;
import com.freewater.cargotracker.infrastructure.persistence.mybatis.dbobject.CarrierMovementPO;

import java.time.LocalDateTime;

public class CarrierMovementConverter {

    public static CarrierMovementPO serialize(Long voyageId, CarrierMovement carrierMovement) {
        if (carrierMovement == null) {
            return null;
        }
        CarrierMovementPO po = new CarrierMovementPO();
        po.setId(carrierMovement.getId());
        po.setVoyageId(voyageId);
        po.setDepartureLocation(LocationConverter.serialize(carrierMovement.getDepartureLocation()));
        po.setDepartureTime(carrierMovement.getDepartureTime());
        po.setArrivalLocation(LocationConverter.serialize(carrierMovement.getArrivalLocation()));
        po.setArrivalTime(carrierMovement.getArrivalTime());
        return po;
    }

    public static CarrierMovement deserialize(CarrierMovementPO carrierMovementPO) {
        if (carrierMovementPO == null) {
            return null;
        }
        final Location departureLocation = LocationConverter.deserialize(carrierMovementPO.getDepartureLocation());
        final LocalDateTime departureTime = carrierMovementPO.getDepartureTime();
        final Location arrivalLocation = LocationConverter.deserialize(carrierMovementPO.getArrivalLocation());
        final LocalDateTime arrivalTime = carrierMovementPO.getArrivalTime();
        final CarrierMovement carrierMovement = new CarrierMovement(departureLocation, arrivalLocation, departureTime, arrivalTime);
        carrierMovement.setId(carrierMovementPO.getId());
        return carrierMovement;
    }
}
